<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px gw-flex-between"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="模型名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery" plain type="primary">重置</el-button>
        <el-button @click="handleQuery" type="primary">查询</el-button>
        <div class="btn-ml">
          <el-button
            type="primary"
            @click="openForm('create')"
            v-hasPermi="['multiModel:large-model:create']"
          >
            新建{{ modelName }}
          </el-button>
        </div>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table :data="list" :max-height="tableMaxHeight" :stripe="true" :show-overflow-tooltip="true" :header-cell-style="{ 'background': '#CCEBE7', 'color': '#585858' }" :align="'center'">
      <el-table-column label="序号" type="index" width="60px" align="center" />
      <el-table-column label="模型名称" align="center" prop="name" />
      <el-table-column label="模型平台" align="center" prop="platform">
        <template #default="scope">
          <span>{{ getDictLabel(DICT_TYPE.MULTI_MODEL_PLATFORM, scope.row.platform) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="model参数" align="center" prop="model" />
      <el-table-column label="训练参数量" align="center" prop="paramNum" width="100px" />
      <el-table-column label="API名称" align="center" prop="apiName" />
      <el-table-column label="API版本" align="center" prop="apiVersion" width="80px" />
      <el-table-column label="调用地址" align="center" prop="apiAddress" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" width="305px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="testModel(scope.row.id, scope.row.platform)"
          >
            测试
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('view', scope.row.id)"
            v-hasPermi="['multiModel:large-model:query']"
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['multiModel:large-model:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            :type="scope.row.status ? 'primary' : 'info'"
            @click="updateStatus(0, scope.row.id)"
            v-hasPermi="['multiModel:large-model:update']"
            :disabled="!scope.row.status"
          >
            启用
          </el-button>
          <el-button
            link
            :type="!scope.row.status ? 'primary' : 'info'"
            @click="updateStatus(1, scope.row.id)"
            v-hasPermi="['multiModel:large-model:update']"
            :disabled="scope.row.status"
          >
            禁用
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['multiModel:large-model:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <LargeModelForm ref="formRef" @success="getList" />  
  <TestModelDialog ref="testModelRef" />
  <DelConfirmDialog ref="delConfirmRef" @confirm="deleteConfirm" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { LargeModelApi, LargeModelVO } from '@/api/multiModel/largemodel'
import LargeModelForm from './LargeModelForm.vue'
import TestModelDialog from './TestModelDialog.vue'
import { getDictLabel, DICT_TYPE } from '@/utils/dict'

/** 模型 列表 */
defineOptions({ name: 'LargeModel' })

const props = defineProps({
  modelName: String,
  modelType: Number
})

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const list = ref<LargeModelVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  type: props.modelType || 0,
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  try {
    const data = await LargeModelApi.getLargeModelPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, props.modelType, props.modelName, id)
}

const testModelRef = ref()
const testModel = async (id: number, platform:string) => {
  testModelRef.value.open(id, platform)
}

/** 删除按钮操作 */
const delConfirmRef = ref()
const handleDelete = async (id: number) => {
  delConfirmRef.value.open(id, ['是否确定删除选中模型?', '删除后模型将无法进行调用测评!'])
}
/** 删除按钮操作 */
const deleteConfirm = async (id: number) => {
  try {
    await LargeModelApi.deleteLargeModel(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const updateStatus = async (status:number, id: number) => {
  try {
    await LargeModelApi.updateLargeModelStatus({ status, id })
    message.success('状态修改成功')
    // 刷新列表
    await getList()
  } catch {}
}

const tableMaxHeight = ref('300px') // 初始高度
// 表格高度
const updateMaxHeight = () => {
  tableMaxHeight.value = document.body.clientHeight - 380 + 'px' // 假设留出100px其他内容空间
}
window.addEventListener('resize', updateMaxHeight)
/** 初始化 **/
onMounted(() => {
  getList()
  updateMaxHeight()
})
onUnmounted(() => {
  window.removeEventListener('resize', updateMaxHeight)
})
</script>
